﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
//using Raize.CodeSiteLogging;
using System.Configuration;
using Pub;
using System.IO;
using System.Data;
using System.Text;


namespace Pub
{
    public static class CGetRainImg
    {
        [DllImport("user32.dll", EntryPoint = "MessageBoxA")]

        static extern int MsgBox(int hWnd, string msg, string caption, int type);



        [DllImport("RainDrawDLL.dll")]

        static extern int LoadConfig(string ConnectionString, string ImgSavePath, string WorkPath);

        [DllImport("RainDrawDLL.dll")]

        static extern int MakeSurfImg(string PStartDate, string PEndDate, string PStartTimes, string PEndTimes, int i_chk_zero);

        public static string getImgName(DateTime dateS, DateTime dateE, string imgPath)
        {
            string SC = "08";
            int i_chk_zero = 0;

            string sDateS = dateS.ToString("yyyyMMdd") + SC;
            string sDateE = dateE.ToString("yyyyMMdd") + SC;

            return getImgName(sDateS, sDateE, SC, imgPath, i_chk_zero);
        }

        public static string getImgName(string dateS, string dateE, string SC, string imgPath, int i_chk_zero)
        {
            CDebug.EnterMethod("后台绘图");
            CDebug.Send(dateS + "," + dateE + "," + SC + "," + imgPath + ", " + i_chk_zero);

            string ImgFile = "";
            try
            {
                string ConnectionString = "Provider=SQLOLEDB.1;" + ConfigurationManager.AppSettings["DBCon"];
                string ImgSavePath = imgPath;
                //string RainDrawPath = ConfigurationManager.AppSettings["RainDrawPath"];
                string WorkPath = imgPath;
                if (LoadConfig(ConnectionString, ImgSavePath, WorkPath) == 0)
                    CDebug.Send("连接成功！");
                else
                    CDebug.Send("连接失败！");

                string StartDate = dateS;      //20120602
                string EndDate = dateE;        //20120603
                string StartTimes = SC;                                                 //08
                string EndTimes = SC;                                                   //08

                int imgTyp = MakeSurfImg(StartDate, EndDate, StartTimes, EndTimes, i_chk_zero);
                if (imgTyp == 0) ImgFile = "";
                else if (imgTyp == 1)
                {
                    ImgFile = imgPath + StartDate.Substring(0, 6) + "/" + EndDate.Substring(0, 8) + "_0808.jpg";
                }
                else if (imgTyp == 2)
                {
                    ImgFile = imgPath + StartDate.Substring(0, 6) + "/" + StartDate.Substring(0, 8) + "_" + EndDate.Substring(0, 8) + "_0808.jpg";
                }

                ImgFile = ImgFile.Replace(@"\", "/");
                ImgFile = ImgFile.Replace("//", "/");

                CDebug.Send(ImgFile);
                //if (ImgFile != string.Empty )

            }
            catch (System.Exception ex)
            {
                CDebug.Send(ex.ToString());
                CDebug.Send("生成图片出错", ex);
            }
            CDebug.ExitMethod("后台绘图");

            return ImgFile;
        }

        /// <summary>
        /// 降水分布图
        /// </summary>
        public static string getRainImg(DateTime dateS, DateTime dateE, string SC, string serverPath, string imgDir, int i_chk_zero)
        {
            CDebug.EnterMethod("生成降水分布图");
            //CDebug.Send(dateS + "," + dateE + "," + SC + "," + serverPath + ", " + i_chk_zero);

            string ImgFile = "";
            try
            {
                string ConnectionString = "Provider=SQLOLEDB.1;" + ConfigurationManager.AppSettings["DBCon"];
                string ImgSavePath = serverPath + imgDir;
                //string RainDrawPath = ConfigurationManager.AppSettings["RainDrawPath"];
                string WorkPath = serverPath + imgDir;
                if (LoadConfig(ConnectionString, ImgSavePath, WorkPath) == 0)
                    CDebug.Send("连接成功！");
                else
                    CDebug.Send("连接失败！");

                string StartDate = dateS.ToString("yyyyMMdd");      //20120602
                string EndDate = dateE.ToString("yyyyMMdd");        //20120603
                string StartTimes = SC;                                                 //08
                string EndTimes = SC;                                                   //08

                int imgTyp = MakeSurfImg(StartDate, EndDate, StartTimes, EndTimes, i_chk_zero);
                if (imgTyp == 0) ImgFile = "";
                else if (imgTyp == 1)
                {
                    ImgFile = imgDir + StartDate.Substring(0, 6) + "/" + EndDate + "_0808.jpg";
                }
                else if (imgTyp == 2)
                {
                    ImgFile = imgDir + StartDate.Substring(0, 6) + "/" + StartDate + "_" + EndDate + "_0808.jpg";
                }

                ImgFile = ImgFile.Replace(@"\", "/");
                ImgFile = ImgFile.Replace("//", "/");

                CDebug.Send(ImgFile);
                //if (ImgFile != string.Empty )

            }
            catch (System.Exception ex)
            {
                CDebug.Send(ex.ToString());
                CDebug.Send("生成图片出错", ex);
            }
            CDebug.ExitMethod("生成降水分布图");

            return ImgFile;
        }

    }

    public static class CGetCiImg
    {
        [DllImport("SurferDLL.dll")]

        static extern int MakeSurfImg(string DataFile);
        //function MakeSurfImg(DataFile: PChar):integer; stdcall;

        /// <summary>
        /// 绘制Ci指数分布图
        /// </summary>
        /// <returns></returns>
        public static string getCiImg(string serverPath, List<string> v)
        {
            CDebug.EnterMethod("Ci指数分布图");
            string ImgFile = "";

            try
            {
                string sTemDataFile = serverPath + CConfig.getPermite("imagesDir") + @"Pub\TemDataCi.txt";
                string sDataPath = serverPath + CConfig.getPermite("imagesDir");
                ImgFile = CConfig.getPermite("imagesDir") + @"Pub\bmp" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bmp";

                List<string> lst = new List<string>();
                CFile.Read(sTemDataFile, ref lst);

                double d_max = Convert.ToDouble(v[0]);
                double d_min = Convert.ToDouble(v[0]);

                for (int i = 1; i < 4; i++)
                {
                    if (d_max < Convert.ToDouble(v[i])) d_max = Convert.ToDouble(v[i]);
                    if (d_min > Convert.ToDouble(v[i])) d_min = Convert.ToDouble(v[i]);
                }
                d_max += 0.1;
                d_min -= 0.1;

                d_max = Math.Round(d_max, 2);
                d_min = Math.Round(d_min, 2);

                lst[0] = lst[0].Replace("{DIR_SRF_DATA}", sDataPath);
                lst[1] = lst[1].Replace("{DIR_IMG}", serverPath + ImgFile);

                lst[4] = lst[4].Replace("{MAX}", "" + d_max);
                lst[5] = lst[5].Replace("{MIN}", "" + d_min);

                lst[14] = lst[14].Replace("{v1}", v[0]);
                lst[15] = lst[15].Replace("{v2}", v[1]);
                lst[16] = lst[16].Replace("{v3}", v[2]);
                lst[17] = lst[17].Replace("{v4}", v[3]);

                string sDataFile = serverPath + CConfig.getPermite("imagesDir") + @"/Pub/DataCi.txt";
                //File.Delete(sDataFile);
                CFile.Write(sDataFile, lst, Encoding.Default, true);
                //string sDataFileII = serverPath + CConfig.getPermite("imagesDir") + "DataCiII.txt";

                //File.Delete(sDataFileII);
                //CFile.UTF8_To_ASCII(sDataFile, sDataFileII);
                //File.Delete(sDataFile);

                int imgTyp = MakeSurfImg(sDataFile);
                CDebug.Send("绘图返回结果：" + imgTyp);


                ImgFile = ImgFile.Replace(@"\", "/");
                ImgFile = ImgFile.Replace("//", "/");

                CDebug.Send(ImgFile);
                //if (ImgFile != string.Empty )

            }
            catch (System.Exception ex)
            {
                CDebug.Send(ex.ToString());
                CDebug.Send("生成图片出错", ex);
            }
            CDebug.ExitMethod("Ci指数分布图");

            return ImgFile;
        }


    }


    public static class CGetMoisImg
    {
        [DllImport("SurferDLL.dll")]

        static extern int MakeSurfImg(string DataFile);
        //function MakeSurfImg(DataFile: PChar):integer; stdcall;

        /// <summary>
        /// 绘制Ci指数分布图
        /// </summary>
        /// <returns></returns>
        public static string getMoisImg(string serverPath, DataSet ds1)
        {
            CDebug.EnterMethod("Ci指数分布图");
            string ImgFile_Bmp = "";
            string ImgFile_Jpg = "";

            try
            {
                string sTemDataFile = serverPath + CConfig.getPermite("imagesDir") + @"Pub\TemDataMois.txt";
                string sDataPath = serverPath + CConfig.getPermite("imagesDir");
                ImgFile_Bmp = CConfig.getPermite("imagesDir") + @"Pub\Mois_Tem" + DateTime.Now.ToString("HHmmss") + ".bmp";
                ImgFile_Jpg = CConfig.getPermite("imagesDir") + @"Pub\Mois" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";

                File.Delete(serverPath + ImgFile_Bmp);
                List<string> lst = new List<string>();
                CFile.Read(sTemDataFile, ref lst);

                double d_max = Convert.ToDouble(ds1.Tables[0].Rows[0]["R"].ToString());
                double d_min = Convert.ToDouble(ds1.Tables[0].Rows[0]["R"].ToString());

                lst[0] = lst[0].Replace("{DIR_SRF_DATA}", sDataPath);
                lst[1] = lst[1].Replace("{DIR_IMG}", serverPath + ImgFile_Bmp);

                List<string> dat_lst = new List<string>();
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    DataRow dr1 = ds1.Tables[0].Rows[i];
                    string sLine1 = dr1["StaID"].ToString() + ","
                        + dr1["lat"].ToString() + ","
                        + dr1["lon"].ToString() + ","
                        + "0,"
                        + dr1["R"].ToString();
                    lst.Add(sLine1);

                    string s_v = dr1["R"].ToString();
                    if (d_max < Convert.ToDouble(s_v)) d_max = Convert.ToDouble(s_v);
                    if (d_min > Convert.ToDouble(s_v)) d_min = Convert.ToDouble(s_v);

                    string sLine2 = dr1["StaID"].ToString() + " "
                        + dr1["lat"].ToString() + " "
                        + dr1["lon"].ToString() + " "
                        + "0 6 12 "
                        + dr1["StaName"].ToString();
                    dat_lst.Add(sLine2);
                }

                string sStaFile = serverPath + CConfig.getPermite("imagesDir") + @"SrfStandard\Mois.dat";
                //File.Delete(sStaFile);
                CFile.Write(sStaFile, dat_lst, Encoding.Default, true);

                //d_max += 0.1;
                //d_min -= 0.1;

                d_max = Math.Round(d_max, 2);
                d_min = Math.Round(d_min, 2);

                lst[4] = lst[4].Replace("{MAX}", "" + d_max);
                lst[5] = lst[5].Replace("{MIN}", "" + d_min);

                string sDataFile = serverPath + CConfig.getPermite("imagesDir") + @"\Pub\DataMois.txt";
                File.Delete(sDataFile);
                CFile.Write(sDataFile, lst, Encoding.Default, true);
                //string sDataFileII = serverPath + CConfig.getPermite("imagesDir") + "DataCiII.txt";

                //File.Delete(sDataFileII);
                //CFile.UTF8_To_ASCII(sDataFile, sDataFileII);
                //File.Delete(sDataFile);

                int imgTyp = MakeSurfImg(sDataFile);
                CDebug.Send("绘图返回结果：" + imgTyp);

                ImgFile_Bmp = ImgFile_Bmp.Replace(@"\", "/");
                ImgFile_Bmp = ImgFile_Bmp.Replace("//", "/");

                ImgFile_Jpg = ImgFile_Jpg.Replace(@"\", "/");
                ImgFile_Jpg = ImgFile_Jpg.Replace("//", "/");

                CPicture.ImageFormatter(serverPath + ImgFile_Bmp, serverPath + ImgFile_Jpg, "jpg");

                //CDebug.Send(ImgFile);
                //if (ImgFile != string.Empty )

            }
            catch (System.Exception ex)
            {
                CDebug.Send(ex.ToString());
                CDebug.Send("生成图片出错", ex);
            }
            CDebug.ExitMethod("Ci指数分布图");

            return ImgFile_Jpg;
            //return ImgFile;
        }

    }

}
